//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-2015, Egret Technology Inc.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
var nest;
(function (nest) {
    var core;
    (function (core) {
        function startup(info, callback) {
            callback({ "result": 0 });
        }
        core.startup = startup;
        function callCustomMethod(customInfo, callback) {
            var data = { module: "core", action: "callCustomMethod", param: customInfo };
            nest.callRuntime(data, callback);
        }
        core.callCustomMethod = callCustomMethod;
    })(core = nest.core || (nest.core = {}));
})(nest || (nest = {}));
/**
 * 登录功能
 *
 * 逻辑：
 * 1.在游戏中展示一张登录背景界面
 * 2.调用 checkLogin 函数判断是否已经登录过，如果登录过，进入步骤6，否则进入步骤3
 * 3.调用 isSupport 函数判断支持的登录类型，根据登录类型显示对应的登录图标
 * 4.用户点击登录图标后，调用 login 函数打开登录面板进行登录
 * 5.如果登录成功，进入步骤6
 * 6.退出登录界面，进入游戏
 *
 *
 * 登出功能：
 *
 * 逻辑：
 * 1.在游戏中放置一个“退出游戏”或者“切换账号”的按钮
 * 2.用户点击“退出游戏”图标后，调用 logout 函数
 * 3.在登出成功后，返回到登录逻辑的步骤1
 *
 *
 */
var nest;
(function (nest) {
    var user;
    (function (user) {
        /**
         * 检测是否已登录
         * @param loginInfo 请传递一个null
         * @param callback
         * @callback-param  @see nest.user.LoginCallbackInfo
         */
        function checkLogin(loginInfo, callback) {
            var data = { module: "user", action: "checkLogin", param: loginInfo };
            nest.callRuntime(data, callback);
        }
        user.checkLogin = checkLogin;
        /**
         * 调用渠道登录接口
         * @param loginInfo
         * @param callback
         * @callback-param  @see nest.user.LoginCallbackInfo
         */
        function login(loginInfo, callback) {
            var data = { module: "user", action: "login", param: loginInfo };
            nest.callRuntime(data, callback, true);
        }
        user.login = login;
        /**
         * 登出接口
         * @param loginInfo 可以传递null
         * @param callback
         * @callback-param   { result : 0 };
         */
        function logout(loginInfo, callback) {
            var nestVersion = egret_native.getOption("egret.runtime.nest");
            if (nestVersion >= 4) {
                var data = { module: "user", action: "logout", param: loginInfo };
                nest.callRuntime(data, callback);
            }
            else {
                callback({ "result": 0 });
            }
        }
        user.logout = logout;
        /**
         * 检测支持何种登录方式
         * @param callback
         * @callback-param  @see nest.user.LoginCallbackInfo
         */
        function isSupport(callback) {
            var data = { module: "user", action: "isSupport" };
            nest.callRuntime(data, callback);
        }
        user.isSupport = isSupport;
    })(user = nest.user || (nest.user = {}));
})(nest || (nest = {}));
var nest;
(function (nest) {
    var iap;
    (function (iap) {
        /**
         * 支付
         * @param orderInfo
         * @param callback
         */
        function pay(orderInfo, callback) {
            var data = { module: "iap", action: "pay", "param": orderInfo };
            nest.callRuntime(data, callback);
        }
        iap.pay = pay;
    })(iap = nest.iap || (nest.iap = {}));
})(nest || (nest = {}));
var nest;
(function (nest) {
    var share;
    (function (_share) {
        /**
         * 是否支持分享
         * @param callback
         * @callback-param {status:0, share:0}  share 1支持 0不支持
         */
        function isSupport(callback) {
            var data = { module: "share", action: "isSupport" };
            nest.callRuntime(data, callback);
        }
        _share.isSupport = isSupport;
        /**
         * 分享
         * @param shareInfo
         * @param callback
         * @callback-param result 0 表示分享成功，-1表示用户取消
         */
        function share(shareInfo, callback) {
            var data = { module: "share", action: "share", "param": shareInfo };
            nest.callRuntime(data, callback, true);
        }
        _share.share = share;
    })(share = nest.share || (nest.share = {}));
})(nest || (nest = {}));
var nest;
(function (nest) {
    var social;
    (function (social) {
        function isSupport(callback) {
            var data = { module: "social", action: "isSupport" };
            nest.callRuntime(data, callback);
        }
        social.isSupport = isSupport;
        function getFriends(socialInfo, callback) {
            var data = { module: "social", action: "getFriends" };
            nest.callRuntime(data, callback);
        }
        social.getFriends = getFriends;
        function openBBS(socialInfo, callback) {
            var data = { module: "social", action: "openBBS" };
            nest.callRuntime(data, callback);
        }
        social.openBBS = openBBS;
    })(social = nest.social || (nest.social = {}));
})(nest || (nest = {}));
var nest;
(function (nest) {
    var app;
    (function (app) {
        /**
         * 是否支持特定功能
         * @param callback
         * @callback-param  { status:"0" , attention :"1" , sendToDesktop : "1"} attention|sendToDesktop 1支持 0不支持
         */
        function isSupport(callback) {
            var data = { module: "app", action: "isSupport" };
            nest.callRuntime(data, callback);
        }
        app.isSupport = isSupport;
        /**
         * 关注
         * @param appInfo
         * @param callback
         */
        function attention(appInfo, callback) {
            var data = { module: "app", action: "attention" };
            nest.callRuntime(data, callback);
        }
        app.attention = attention;
        /**
         * 退出游戏，回到 App 界面
         * @param appInfo
         * @param callback
         */
        function exitGame(appInfo, callback) {
            var data = { module: "app", action: "exitGame" };
            nest.callRuntime(data, callback);
        }
        app.exitGame = exitGame;
        /**
         * 发送到桌面
         * @param appInfo
         * @param callback
         * @param callback-param result 0表示添加桌面成功，-1表示添加失败
         */
        function sendToDesktop(appInfo, callback) {
            var data = { module: "app", action: "sendToDesktop" };
            nest.callRuntime(data, callback);
        }
        app.sendToDesktop = sendToDesktop;
    })(app = nest.app || (nest.app = {}));
})(nest || (nest = {}));
var nest;
(function (nest) {
    var externalArr = [];
    function callRuntime(data, callback, parallel) {
        if (parallel === void 0) { parallel = false; }
        var tag = "nest";
        if (parallel) {
            egret.ExternalInterface.addCallback(tag, function (data) {
                console.log(data);
                var obj = JSON.parse(data);
                callback(obj.data);
            });
            egret.ExternalInterface.call(tag, JSON.stringify(data));
        }
        else {
            externalArr.push({ "data": data, "callback": callback });
            _getData();
        }
    }
    nest.callRuntime = callRuntime;
    var isRunning = false;
    function _getData() {
        if (externalArr.length) {
            if (isRunning) {
                return;
            }
            isRunning = true;
            var info = externalArr.shift();
            var tag = "nest";
            egret.ExternalInterface.addCallback(tag, function (data) {
                console.log(data);
                var obj = JSON.parse(data);
                info["callback"](obj.data);
                isRunning = false;
                _getData();
            });
            egret.ExternalInterface.call(tag, JSON.stringify(info["data"]));
        }
    }
})(nest || (nest = {}));
